package com.cljz.makepassenger.ui.activity;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Html;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.cljz.makepassenger.base.App;
import com.cljz.makepassenger.R;
import com.cljz.makepassenger.base.BaseActivity;
import com.cljz.makepassenger.base.Constant;
import com.cljz.makepassenger.event.CollectEvent;
import com.cljz.makepassenger.event.RefreshEvent;
import com.cljz.makepassenger.model.PublishReqBean;
import com.cljz.makepassenger.model.net.bean.GetTypeListBean;
import com.cljz.makepassenger.model.net.bean.InvitationBean;
import com.cljz.makepassenger.model.net.bean.InviteDetailBean;
import com.cljz.makepassenger.model.net.bean.PublishBean;
import com.cljz.makepassenger.model.net.bean.UploadImgBean;
import com.cljz.makepassenger.present.activity.InvitationLetterP;
import com.cljz.makepassenger.share.ShareManager;
import com.cljz.makepassenger.ui.ItemDecoration.SpacesItemDecoration;
import com.cljz.makepassenger.ui.adapter.ActivityPicPagerAdapter;
import com.cljz.makepassenger.ui.adapter.InvitationShareAdapter;
import com.cljz.makepassenger.ui.adapter.LetterBgPicAdapter;
import com.cljz.makepassenger.ui.adapter.PersonListAdapter;
import com.cljz.makepassenger.ui.adapter.PicTypeAdapter;
import com.cljz.makepassenger.utils.ActivityManager;
import com.cljz.makepassenger.utils.L;
import com.cljz.makepassenger.utils.ScreenshotUtils;
import com.cljz.makepassenger.utils.T;
import com.cljz.makepassenger.utils.date.GetSystemTimeUtil;
import com.cljz.makepassenger.widget.OkDialogView;
import com.cljz.makepassenger.widget.PullUpToLoadMore;
import com.jakewharton.rxbinding.view.RxView;
import com.squareup.picasso.Callback;
import com.squareup.picasso.Picasso;
import com.umeng.socialize.ShareAction;
import com.umeng.socialize.UMShareAPI;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.media.UMImage;
import com.umeng.socialize.media.UMWeb;

import java.io.File;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import cn.droidlover.xdroidmvp.cache.SharedPref;
import cn.droidlover.xdroidmvp.event.BusProvider;
import cn.droidlover.xdroidmvp.router.Router;
import rx.functions.Action1;

import static com.cljz.makepassenger.R.id.layout_activity_details;

/**
 * Created by apple on 2017/4/7.
 */

public class InvitationLetterActivity extends BaseActivity<InvitationLetterP> implements PullUpToLoadMore.ScrollListener, ViewPager.OnPageChangeListener, ActivityPicPagerAdapter.OnItemClickListener, PicTypeAdapter.OnItemClickListener, LetterBgPicAdapter.OnItemClickListener {
    private static final int EDITOR_USER_INFO = 666;
    @BindView(R.id.layout_invite)
    View layoutInvite;
    @BindView(layout_activity_details)
    View layoutActivityDetails;
    @BindView(R.id.iv_bg)
    ImageView ivBg;
    @BindView(R.id.tv_letter_title)
    TextView tvLetterTitle;
    @BindView(R.id.tv_letter_who)
    TextView tvLetterWho;
    @BindView(R.id.tv_letter_things)
    TextView tvLetterThings;
    @BindView(R.id.tv_letter_time)
    TextView tvLetterTime;
    @BindView(R.id.tv_letter_week)
    TextView tvLetterWeek;
    @BindView(R.id.tv_letter_where)
    TextView tvLetterWhere;
    @BindView(R.id.tv_letter_address)
    TextView tvLetterAddress;
    @BindView(R.id.fl_contain)
    FrameLayout flContain;
    @BindView(R.id.iv_right_person)
    ImageView ivRightPerson;
    @BindView(R.id.iv_right_map)
    ImageView ivRightMap;
    @BindView(R.id.iv_right_edit)
    ImageView ivRightEdit;
    @BindView(R.id.iv_right_cancel)
    ImageView ivRightCancel;
    @BindView(R.id.iv_right_share)
    ImageView ivRightShare;
    @BindView(R.id.iv_right_collect)
    ImageView ivRightCollect;
    @BindView(R.id.ll_right)
    LinearLayout llRight;
    @BindView(R.id.vp_activity_pic)
    ViewPager vpActivityPic;
    @BindView(R.id.tv_pic_num)
    TextView tvPicNum;
    @BindView(R.id.rl_activity_pic)
    RelativeLayout rlActivityPic;
    @BindView(R.id.iv_activity_title)
    ImageView ivActivityTitle;
    @BindView(R.id.tv_activity_title)
    TextView tvActivityTitle;
    @BindView(R.id.iv_activity_time)
    ImageView ivActivityTime;
    @BindView(R.id.tv_activity_time)
    TextView tvActivityTime;
    @BindView(R.id.iv_activity_address)
    ImageView ivActivityAddress;
    @BindView(R.id.tv_activity_location)
    TextView tvActivityLocation;
    @BindView(R.id.rl_activity_location)
    RelativeLayout rlActivityLocation;
    @BindView(R.id.iv_activity_numbers)
    ImageView ivActivityNumbers;
    @BindView(R.id.tv_activity_num)
    TextView tvActivityNum;
    @BindView(R.id.rl_activity_num)
    RelativeLayout rlActivityNum;
    @BindView(R.id.tv_activity_registration_time)
    TextView tvActivityRegistrationTime;
    @BindView(R.id.tv_activity_des)
    TextView tvActivityDes;
    @BindView(R.id.putlm)
    PullUpToLoadMore putlm;
    @BindView(R.id.tv_submit_invitation)
    TextView tvSubmitInvitation;
    @BindView(R.id.fl_back)
    FrameLayout flBack;
    @BindView(R.id.iv_change_bg_button)
    ImageView ivChangeBgButton;
    @BindView(R.id.rv_bg_type)
    RecyclerView rvBgType;
    @BindView(R.id.rv_bg)
    RecyclerView rvBg;
    @BindView(R.id.rl_change_bg)
    RelativeLayout rlChangeBg;
    @BindView(R.id.rl_activity_share)
    RelativeLayout rlActivityShare;
    @BindView(R.id.iv_activity_collect)
    ImageView ivActivityCollect;
    @BindView(R.id.rl_activity_collect_or_cancel)
    RelativeLayout rlActivityCollectOrCancel;
    @BindView(R.id.btn_activity_join)
    Button btnActivityJoin;
    @BindView(R.id.rl_activity_menu)
    RelativeLayout rlActivityMenu;
    @BindView(R.id.fl_loading)
    FrameLayout flLoading;
    @BindView(R.id.rl_invitation_letter)
    RelativeLayout rlInvitationLetter;
    @BindView(R.id.iv_activity_location_next)
    ImageView ivActivityLocationNext;
    @BindView(R.id.iv_activity_num_next)
    ImageView ivActivityNumNext;
    @BindView(R.id.rl_guidance)
    RelativeLayout rlGuidance;
    @BindView(R.id.iv_guidance)
    ImageView ivGuidance;
    @BindView(R.id.ll_activity_collect)
    LinearLayout llActivityCollect;
    @BindView(R.id.ll_activity_cancel)
    LinearLayout llActivityCancel;
    private LetterBgPicAdapter picAdapter;
    private String picSelectId;
    private String share_url;
    private PopupWindow shareWindow;
    private ScreenshotUtils screenshotUtils;
    private String projectId;
    private OkDialogView okDialogView;
    private InviteDetailBean.DataBean inviteDetailDataBean;
    private PopupWindow UserDialogWindow;
    private Boolean outTime;
    public LocationClient mLocationClient = null;
    public BDLocationListener myListener = new MyLocationListener();
    private static double latitude;
    private static double longitude;
    private PublishReqBean publishReqBean;
    private int type;
    private ArrayList<String> picList;

    public static final int EDITOR = 0;
    public static final int INVITE = 1;
    public static final int ACTIVITY = 2;
    private boolean hasCollected;
    private PicTypeAdapter picTypeAdapter;
    private List<GetTypeListBean.DataBean> typeList;
    private List<List<InvitationBean.DataBean>> invitationList;
    private boolean isOwn;
    private String inviteId;
    private ActivityPicPagerAdapter activityPicPagerAdapter;
    public static final String TAG = "InvitationLetterActivity";

    @Override
    public void initData(Bundle savedInstanceState) {
        showLoading();
        type = getIntent().getExtras().getInt("type", -1);
        llRight.setAlpha(0.8f);
        screenshotUtils = new ScreenshotUtils(this);
        initLocation();
        if (type == EDITOR) {
            issueInvite();
        } else {
            projectId = getIntent().getExtras().getString("id");
            tvSubmitInvitation.setVisibility(View.GONE);
            llRight.setVisibility(View.GONE);
            ivChangeBgButton.setVisibility(View.GONE);
            getP().getInviteDetail(projectId, type);
        }
    }

    private void initLocation() {

        //声明LocationClient类
        mLocationClient = new LocationClient(getApplicationContext());
        //注册监听函数
        mLocationClient.registerLocationListener(myListener);
        LocationClientOption option = new LocationClientOption();
        option.setLocationMode(LocationClientOption.LocationMode.Hight_Accuracy);
        //可选，默认高精度，设置定位模式，高精度，低功耗，仅设备

        option.setCoorType("bd09ll");
        //可选，默认gcj02，设置返回的定位结果坐标系

        int span = 0;
        option.setScanSpan(span);
        //可选，默认0，即仅定位一次，设置发起定位请求的间隔需要大于等于1000ms才是有效的

        option.setIsNeedAddress(true);
        //可选，设置是否需要地址信息，默认不需要

        option.setOpenGps(true);
        //可选，默认false,设置是否使用gps

        option.setLocationNotify(false);
        //可选，默认false，设置是否当GPS有效时按照1S/1次频率输出GPS结果

        option.setIsNeedLocationDescribe(true);
        //可选，默认false，设置是否需要位置语义化结果，可以在BDLocation.getLocationDescribe里得到，结果类似于“在北京天安门附近”

        option.setIsNeedLocationPoiList(true);
        //可选，默认false，设置是否需要POI结果，可以在BDLocation.getPoiList里得到

        option.setIgnoreKillProcess(false);
        //可选，默认true，定位SDK内部是一个SERVICE，并放到了独立进程，设置是否在stop的时候杀死这个进程，默认不杀死

        option.SetIgnoreCacheException(false);
        //可选，默认false，设置是否收集CRASH信息，默认收集

        option.setEnableSimulateGps(false);
        //可选，默认false，设置是否需要过滤GPS仿真结果，默认需要

        mLocationClient.setLocOption(option);
        //注册监听函数
        mLocationClient.registerLocationListener(myListener);
    }

    @Override
    protected void onStart() {
        super.onStart();
        mLocationClient.start();
    }

    @Override
    protected void onStop() {
        super.onStop();
        mLocationClient.stop();
    }

    @Override
    protected void onDestroy() {
        UMShareAPI.get(this).release();
        super.onDestroy();

    }

    @Override
    protected void initListener() {
        super.initListener();
        putlm.setScrollListener(this);
        RxView.clicks(ivChangeBgButton)
                .compose(this.<Void>bindToLifecycle())
                .subscribe(new Action1<Void>() {
                    @Override
                    public void call(Void aVoid) {
                        rlChangeBg.setVisibility(View.VISIBLE);
                        tvSubmitInvitation.setVisibility(View.GONE);
                        ivChangeBgButton.setVisibility(View.GONE);
                    }
                });
        RxView.clicks(rlChangeBg)
                .compose(this.<Void>bindToLifecycle())
                .subscribe(new Action1<Void>() {
                    @Override
                    public void call(Void aVoid) {
                        rlChangeBg.setVisibility(View.GONE);
                        tvSubmitInvitation.setVisibility(View.VISIBLE);
                        if (getIntent().getExtras().getInt("type") == 0) {
                            ivChangeBgButton.setVisibility(View.VISIBLE);
                        }
                    }
                });
        RxView.clicks(tvSubmitInvitation)
                .compose(this.<Void>bindToLifecycle())
                .subscribe(new Action1<Void>() {
                    @Override
                    public void call(Void aVoid) {
                        if (type == EDITOR) {
                            if (share_url == null) {
//                                showLoading();
                                showLoadingDialog();
                                publishReqBean.setInvitation_id(picSelectId);
                                if ("1".equals(publishReqBean.getIs_activity())) {
                                    getP().upLoadImg(picList);
                                } else {
                                    getP().publish(publishReqBean);
                                }
//                                tvSubmitInvitation.setEnabled(false);
//                                tvSubmitInvitation.setText("图片上传中");
                            } else {
                                initShare(share_url);
                            }
                        } else if (type == INVITE) {
                            if (share_url != null) {
                                initShare(share_url);
                            }
                        } else {
                            if ("1".equals(inviteDetailDataBean.getInvite().getIs_own())) {
                                if (share_url != null) {
                                    initShare(share_url);
                                }
                            } else {
                                Router.newIntent((Activity) App.getInstance())
                                        .to(EditorInformationActivity.class)
                                        .putString("activity_id", projectId)
                                        .requestCode(EDITOR_USER_INFO)
                                        .launch();
                            }
                        }
                    }
                });
        RxView.clicks(flBack)
                .compose(this.<Void>bindToLifecycle())
                .subscribe(new Action1<Void>() {
                    @Override
                    public void call(Void aVoid) {
                        if (getIntent().getExtras().getInt("type", 0) == 0) {
                            if (share_url != null) {
                                Router.newIntent((Activity) App.getInstance()).to(MainActivity.class).launch();
                                finish();
                            } else {
                                finish();
                            }
                        } else {
                            finish();
                        }
                    }
                });
        RxView.clicks(ivRightCancel)
                .compose(this.<Void>bindToLifecycle())
                .subscribe(new Action1<Void>() {
                    @Override
                    public void call(Void aVoid) {
                        cancelInvitation();
                    }
                });
        RxView.clicks(ivRightEdit)
                .compose(this.<Void>bindToLifecycle())
                .subscribe(new Action1<Void>() {
                    @Override
                    public void call(Void aVoid) {
                        Router.newIntent((Activity) App.getInstance()).to(EditorInvitedActivity.class).putInt("type", 1).putSerializable("data", inviteDetailDataBean).putString("invite_id", projectId).requestCode(11).launch();
                    }
                });
        RxView.clicks(ivRightMap)
                .compose(this.<Void>bindToLifecycle())
                .subscribe(new Action1<Void>() {
                    @Override
                    public void call(Void aVoid) {
                        showMapList();
                    }
                });
        RxView.clicks(ivRightPerson)
                .compose(this.<Void>bindToLifecycle())
                .subscribe(new Action1<Void>() {
                    @Override
                    public void call(Void aVoid) {
                        showUserList();
                    }
                });

        //activity
        RxView.clicks(ivRightShare)
                .compose(this.<Void>bindToLifecycle())
                .subscribe(new Action1<Void>() {
                    @Override
                    public void call(Void aVoid) {
                        initShare(share_url);
                    }
                });

        RxView.clicks(rlActivityLocation)
                .compose(this.<Void>bindToLifecycle())
                .subscribe(new Action1<Void>() {
                    @Override
                    public void call(Void aVoid) {
                        if (type == ACTIVITY) {
                            Router.newIntent((Activity) App.getInstance())
                                    .putSerializable("inviteDetail", inviteDetailDataBean)
                                    .to(ActivityMapActivity.class)
                                    .launch();
                        }
                    }
                });
        RxView.clicks(rlActivityNum)
                .compose(this.<Void>bindToLifecycle())
                .subscribe(new Action1<Void>() {
                    @Override
                    public void call(Void aVoid) {
                        if (type == ACTIVITY) {
                            Router.newIntent((Activity) App.getInstance())
                                    .to(MemberListActivity.class)
                                    .putInt("type", type)
                                    .putString("id", projectId)
                                    .launch();
                        }
                    }
                });
        RxView.clicks(rlActivityShare)
                .compose(this.<Void>bindToLifecycle())
                .subscribe(new Action1<Void>() {
                    @Override
                    public void call(Void aVoid) {
                        initShare(share_url);
                    }
                });
        RxView.clicks(btnActivityJoin)
                .compose(this.<Void>bindToLifecycle())
                .subscribe(new Action1<Void>() {
                    @Override
                    public void call(Void aVoid) {
                        if ("1".equals(inviteDetailDataBean.getInvite().getIs_own())) {
                            if (share_url != null) {
                                initShare(share_url);
                            }
                        } else {
                            Router.newIntent((Activity) App.getInstance())
                                    .to(EditorInformationActivity.class)
                                    .putString("activity_id", projectId)
                                    .requestCode(EDITOR_USER_INFO)
                                    .launch();
                        }
                    }
                });
        RxView.clicks(rlActivityCollectOrCancel)
                .compose(this.<Void>bindToLifecycle())
                .subscribe(new Action1<Void>() {
                    @Override
                    public void call(Void aVoid) {
                        if ("1".equals(inviteDetailDataBean.getInvite().getIs_own())) {
                            cancelInvitation();
                        } else {
                            changeCollect();
                        }
                    }
                });
        RxView.clicks(ivRightCollect)
                .compose(this.<Void>bindToLifecycle())
                .subscribe(new Action1<Void>() {
                    @Override
                    public void call(Void aVoid) {
                        changeCollect();
                    }
                });
        RxView.clicks(ivGuidance)
                .compose(this.<Void>bindToLifecycle())
                .subscribe(new Action1<Void>() {
                    @Override
                    public void call(Void aVoid) {
                        SharedPref.getInstance(App.getInstance()).putBoolean(Constant.HAS_GUIDANCE, true);
                        rlGuidance.setVisibility(View.GONE);
                        tvSubmitInvitation.setVisibility(View.VISIBLE);
                    }
                });
    }

    private void changeCollect() {
        if (hasCollected) {
            getP().collectionCancel(projectId);
        } else {
            getP().collection(projectId);
        }
    }

    /**
     * 邀请人员
     */
    private void showUserList() {
        View personDialogView = LayoutInflater.from(this).inflate(R.layout.pop_person_view, null);
        UserDialogWindow = new PopupWindow(personDialogView, WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.MATCH_PARENT, true);
        UserDialogWindow.setTouchable(true);
        UserDialogWindow.setTouchInterceptor(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                return false;
            }
        });
        ColorDrawable dw = new ColorDrawable(0xb0000000);
        UserDialogWindow.setBackgroundDrawable(dw);
        UserDialogWindow.showAtLocation(flContain, Gravity.CENTER | Gravity.CENTER_HORIZONTAL, 0, 0);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
        RecyclerView personRecycleView = (RecyclerView) personDialogView.findViewById(R.id.rv_person_list);
        personRecycleView.setLayoutManager(linearLayoutManager);
        PersonListAdapter personListAdapter = new PersonListAdapter(this, inviteDetailDataBean.getInvite().getMember());
        personRecycleView.setAlpha(0.8f);
        personRecycleView.setAdapter(personListAdapter);
        RelativeLayout picUserView = (RelativeLayout) personDialogView.findViewById(R.id.rl_layout);
        picUserView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                UserDialogWindow.dismiss();
            }
        });
    }

    private void showMapList() {
        Dialog dialog = null;
//        final AddrModel model = gson.fromJson(PreferenceManager.getInstance().getLocationMsg(), AddrModel.class);
        AlertDialog.Builder builder = new AlertDialog.Builder(this, AlertDialog.THEME_DEVICE_DEFAULT_LIGHT);
        builder.setTitle("选择地图打开").setSingleChoiceItems(new String[]{"百度地图", "高德地图"}, 0, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Intent intent = null;
                try {
                    if (which == 0) {
                        StringBuilder loc = new StringBuilder();
                        loc.append("intent://map/direction?origin=latlng:");
                        loc.append(latitude);
                        loc.append(",");
                        loc.append(longitude);
                        loc.append("|name:");
                        loc.append("我的位置");
                        loc.append("&destination=latlng:");
                        loc.append(inviteDetailDataBean.getInvite().getLatitude());
                        loc.append(",");
                        loc.append(inviteDetailDataBean.getInvite().getLongitude());
                        loc.append("|name:");
                        loc.append(inviteDetailDataBean.getInvite().getAddress());
                        loc.append("&mode=driving");
                        loc.append("&referer=Autohome|GasStation#Intent;scheme=bdapp;package=com.baidu.BaiduMap;end");
                        intent = Intent.getIntent(loc.toString());
                        if (isInstallByRead("com.baidu.BaiduMap")) {
                            startActivity(intent); //启动调用
                            dialog.dismiss();
                        } else {
                            T.getInstance().showShort("没有安装百度地图客户端");
//                            showToast("没有安装百度地图客户端", R.mipmap.jinshi);
                        }
                    } else {
                        double[] gd_lat_lon = bdToGaoDe(Double.valueOf(inviteDetailDataBean.getInvite().getLatitude()), Double.valueOf(inviteDetailDataBean.getInvite().getLatitude()));
                        StringBuilder loc = new StringBuilder();
                        loc.append("androidamap://viewMap?sourceApplication=XX");
                        loc.append("&poiname=");
                        loc.append(inviteDetailDataBean.getInvite().getAddress());
                        loc.append("&lat=");
                        loc.append(gd_lat_lon[0]);
                        loc.append("&lon=");
                        loc.append(gd_lat_lon[1]);
                        loc.append("&dev=0");
                        intent = Intent.getIntent(loc.toString());
                        if (isInstallByRead("com.autonavi.minimap")) {
                            startActivity(intent); //启动调用
                            dialog.dismiss();
                        } else {
                            T.getInstance().showShort("没有安装高德地图客户端");

//                            showToast("没有安装高德地图客户端", R.mipmap.jinshi);

                        }
                    }
                } catch (URISyntaxException e) {
                    e.printStackTrace();
                }
            }
        });
        dialog = builder.create();
        dialog.setCanceledOnTouchOutside(true);
        dialog.show();
    }

    private void cancelInvitation() {
        okDialogView = new OkDialogView(this, outTime ? "您确定删除该邀请，删除后记录将消失？" : (isOwn ? "若您取消邀请，我们将通知受邀人，确认取消？" : "若撤销参与，我们将通知邀请人，确认撤销？"), R.style.MyDialog, new OkDialogView.YesDialogListener() {
            @Override
            public void onClick(View v) {
                switch (v.getId()) {
                    case R.id.tv_cancel:
                        okDialogView.dismiss();
                        break;
                    case R.id.tv_yes:
                        getP().getInviteCancel(inviteId);
                        L.e("project", inviteId);
                }
            }
        });
        okDialogView.show();
    }

    private void initShare(final String share_url) {
        ivChangeBgButton.setVisibility(View.GONE);
        View shareView = LayoutInflater.from(this).inflate(R.layout.pop_share, null);
        int[] shareIcon = {R.mipmap.wechat, R.mipmap.friends, R.mipmap.qq, R.mipmap.qzone};
        RecyclerView rvShare = (RecyclerView) shareView.findViewById(R.id.rv_share);
        FrameLayout flLayout = (FrameLayout) shareView.findViewById(R.id.fl_layout);
        GridLayoutManager gridLayoutManager = new GridLayoutManager(this, 4);
        rvShare.setLayoutManager(gridLayoutManager);
        InvitationShareAdapter invitationShareAdapter = new InvitationShareAdapter(InvitationLetterActivity.this, shareIcon);
        shareWindow = new PopupWindow(shareView, WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.MATCH_PARENT, true);
        shareWindow.setTouchable(true);
        shareWindow.setTouchInterceptor(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                return false;
            }
        });
        flLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                shareWindow.dismiss();
            }
        });
        rvShare.setAlpha(0.8f);
        rvShare.setAdapter(invitationShareAdapter);
        ColorDrawable dw = new ColorDrawable(0xb0000000);
        shareWindow.setBackgroundDrawable(dw);
        shareWindow.showAtLocation(rlInvitationLetter, Gravity.CENTER | Gravity.CENTER_HORIZONTAL, 0, 0);
        invitationShareAdapter.setOnItemClickListener(new InvitationShareAdapter.OnItemClickListener() {
            @Override
            public void onClick(int position) {
                String nikeName = getP().getLocalUserInfo(getP().getToken()).getUser_nickname();
                Bitmap bitmap = screenshotUtils.getBitmapByView(flContain);//这里返回的是没有水印的图片
                Bitmap backdrop = BitmapFactory.decodeResource(InvitationLetterActivity.this.getResources(), R.mipmap.zhegai);
                Bitmap bitmap2 = screenshotUtils.addTagUtil(bitmap, screenshotUtils.generate(InvitationLetterActivity.this, share_url), backdrop, InvitationLetterActivity.this);//返回二维码的水印
                UMImage image = new UMImage(InvitationLetterActivity.this, bitmap2);
                UMWeb shareWeb = new UMWeb(share_url);
                String shareTitle = "";
                if (type == EDITOR) {
                    if ("1".equals(publishReqBean.getIs_activity())) {
                        shareTitle = publishReqBean.getTitle2();
                    } else {
                        shareTitle = publishReqBean.getTitle();
                    }
                } else if (type == ACTIVITY) {
                    shareTitle = inviteDetailDataBean.getActivity().getTitle();
                } else {
                    shareTitle = inviteDetailDataBean.getInvite().getTitle();
                }
                shareWeb.setTitle(shareTitle);
                shareWeb.setDescription("1Q-超好用的邀请工具");
                shareWeb.setThumb(new UMImage(InvitationLetterActivity.this, getP().getLocalUserInfo(getP().getToken()).getUser_logo()));
                switch (position) {
                    case 2:
                        try {
                            new ShareAction(InvitationLetterActivity.this).setPlatform(SHARE_MEDIA.QQ).setCallback(new ShareManager(InvitationLetterActivity.this))
                                    .withMedia(shareWeb)
                                    .share();
                            shareWindow.dismiss();
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                        break;
                    case 0:
                        try {
                            new ShareAction(InvitationLetterActivity.this).setPlatform(SHARE_MEDIA.WEIXIN).setCallback(new ShareManager(InvitationLetterActivity.this))
                                    .withMedia(shareWeb)
                                    .share();
                            shareWindow.dismiss();
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                        break;
                    case 3:
                        try {
                            new ShareAction(InvitationLetterActivity.this).setPlatform(SHARE_MEDIA.QZONE).setCallback(new ShareManager(InvitationLetterActivity.this))
                                    .withMedia(image)
                                    .withText(nikeName + "通过1Q给你发起了邀请")
                                    .share();
                            shareWindow.dismiss();
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                        break;
                    case 1:
                        try {
                            new ShareAction(InvitationLetterActivity.this).setPlatform(SHARE_MEDIA.WEIXIN_CIRCLE).setCallback(new ShareManager(InvitationLetterActivity.this))
                                    .withMedia(image)
                                    .withText(nikeName + "通过1Q给你发起了邀请")
                                    .share();
                            shareWindow.dismiss();
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                        break;
                }
            }

            @Override
            public void onLongClick(int position) {
            }
        });
    }

    //编辑邀请函
    private void issueInvite() {
        publishReqBean = getIntent().getExtras().getParcelable("data");
        llRight.setVisibility(View.GONE);
        tvSubmitInvitation.setVisibility(View.GONE);
        // TODO: 2017/6/15   layoutInvite.setVisibility(View.VISIBLE);
        if ("1".equals(publishReqBean.getIs_activity())) {
            layoutActivityDetails.setVisibility(View.VISIBLE);
            if (publishReqBean.getPicList() != null) {
                picList = publishReqBean.getPicList();
            } else {
                picList = new ArrayList<>();
            }
            if (null != publishReqBean.getCoverBitmapPath()) {
                picList.add(0, publishReqBean.getCoverBitmapPath());
            }
            if (picList.size() > 0) {
                initActivityPic();
            }
            Boolean hasGuidance = SharedPref.getInstance(App.getInstance()).getBoolean(Constant.HAS_GUIDANCE, false);
            if (hasGuidance) {
                rlGuidance.setVisibility(View.GONE);
                tvSubmitInvitation.setVisibility(View.VISIBLE);
            } else {
                rlGuidance.setVisibility(View.VISIBLE);
                tvSubmitInvitation.setVisibility(View.GONE);
            }
        } else {
            layoutActivityDetails.setVisibility(View.GONE);
        }
        getP().getTypeList();
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_invitation_letter;
    }

    @Override
    public InvitationLetterP newP() {
        return new InvitationLetterP();
    }

    @Override
    protected void setScreen() {
        super.setScreen();
        WindowManager.LayoutParams lp = getWindow().getAttributes();
        lp.flags = (WindowManager.LayoutParams.FLAG_FULLSCREEN);
        getWindow().setAttributes(lp);
        getWindow().clearFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
    }

    public void showLetterBgFail(String error) {

    }

    private void dismissLoading() {
        flLoading.setVisibility(View.GONE);
//        backgroundAlpha(1.0f);
    }

    private void showLoading() {
        flLoading.setVisibility(View.VISIBLE);
//        backgroundAlpha(0.8f);
    }


    public void initActivityPic() {
        activityPicPagerAdapter = new ActivityPicPagerAdapter(InvitationLetterActivity.this, picList, type,TAG);
        tvPicNum.setText(1 + "/" + picList.size());
        vpActivityPic.setAdapter(activityPicPagerAdapter);
        activityPicPagerAdapter.setOnItemClickListener(this);
        vpActivityPic.setOnPageChangeListener(InvitationLetterActivity.this);
    }

    private void changeTextColor(String textColor) {
        tvLetterWho.setTextColor(Color.parseColor(textColor));
        tvLetterThings.setTextColor(Color.parseColor(textColor));
        tvLetterTime.setTextColor(Color.parseColor(textColor));
        tvLetterWeek.setTextColor(Color.parseColor(textColor));
        tvLetterWhere.setTextColor(Color.parseColor(textColor));
        tvLetterAddress.setTextColor(Color.parseColor(textColor));
    }


    public void inviteDetailSucceed(final InviteDetailBean.DataBean data) {
        dismissLoading();
        inviteDetailDataBean = data;
        Picasso.with(this).load(data.getInvite().getAd_logo()).error(R.color.white).placeholder(R.color.white).into(ivBg, new Callback() {
            @Override
            public void onSuccess() {
                llRight.setVisibility(View.VISIBLE);
                tvSubmitInvitation.setVisibility(View.VISIBLE);
                tvLetterThings.setText(inviteDetailDataBean.getInvite().getTitle());
                tvLetterTime.setText(inviteDetailDataBean.getInvite().getDate() + " " + inviteDetailDataBean.getInvite().getTime());
                tvLetterWeek.setText(inviteDetailDataBean.getInvite().getWeek());
                tvLetterWhere.setText(inviteDetailDataBean.getInvite().getLandmark());
                tvLetterAddress.setText(inviteDetailDataBean.getInvite().getAddress());
                tvLetterWho.setText("来自 " + inviteDetailDataBean.getInvite().getUser_nickname() + "  的邀请");
                if (!"".equals(inviteDetailDataBean.getInvite().getColor())) {
                    changeTextColor(inviteDetailDataBean.getInvite().getColor());
                }
                share_url = inviteDetailDataBean.getInvite().getShare_url();
                inviteId = inviteDetailDataBean.getInvite().getInvite_id();
                if ("1".equals(inviteDetailDataBean.getIs_activity())) {
                    type = ACTIVITY;
                    projectId = inviteDetailDataBean.getActivity().getActivity_id();
                    initActivityLetter();
                } else {
                    type = INVITE;
                    initInviteLetter();
                }
            }

            @Override
            public void onError() {
//                dismissLoading();
                dismissLoadingDialog();
            }
        });
    }

    public void initInviteLetter() {
        layoutInvite.setVisibility(View.VISIBLE);
        layoutActivityDetails.setVisibility(View.GONE);
        ivRightShare.setVisibility(View.GONE);
        ivRightCollect.setVisibility(View.GONE);
        ivRightEdit.setVisibility("1".equals(inviteDetailDataBean.getInvite().getIs_own()) ? View.VISIBLE : View.GONE);
        if ("1".equals(inviteDetailDataBean.getInvite().getIs_expire())) {
            tvSubmitInvitation.setText("已过期");
            outTime = true;
            tvSubmitInvitation.setEnabled(false);
        } else {
            outTime = false;
            if ("2".equals(inviteDetailDataBean.getInvite().getIs_member())) {
                tvSubmitInvitation.setText("已取消");
                tvSubmitInvitation.setEnabled(false);
            } else {
                isOwn = "0".equals(inviteDetailDataBean.getInvite().getIs_own());
                tvSubmitInvitation.setText("继续邀请");
                tvSubmitInvitation.setEnabled(true);
            }
        }


    }

    public void initActivityLetter() {
        layoutActivityDetails.setVisibility(View.VISIBLE);
        // TODO: 2017/6/15 活动不展示邀请函 将visible改为gone 布局中layoutInvite改为gone
        layoutInvite.setVisibility(View.VISIBLE);
        Boolean hasGuidance = SharedPref.getInstance(App.getInstance()).getBoolean(Constant.HAS_GUIDANCE, false);
        if (hasGuidance) {
            rlGuidance.setVisibility(View.GONE);
            tvSubmitInvitation.setVisibility(View.VISIBLE);
        } else {
            rlGuidance.setVisibility(View.VISIBLE);
            tvSubmitInvitation.setVisibility(View.GONE);
        }
        tvActivityTitle.setText(inviteDetailDataBean.getActivity().getTitle());
        tvActivityLocation.setText(inviteDetailDataBean.getActivity().getAddress());
        tvActivityDes.setText(inviteDetailDataBean.getActivity().getContent());
        tvActivityTime.setText(inviteDetailDataBean.getActivity().getActivity_time());
        tvActivityRegistrationTime.setText(inviteDetailDataBean.getActivity().getTime_start() + "-" + inviteDetailDataBean.getActivity().getTime_end());
        tvActivityNum.setText(Html.fromHtml("已有<font color='" + getResources().getColor(R.color.x_red) + "'>" + inviteDetailDataBean.getActivity().getNumber_join() + "人</font>参加/限" + inviteDetailDataBean.getActivity().getNumber_all() + "人参加"), TextView.BufferType.SPANNABLE);
        if ("1".equals(inviteDetailDataBean.getActivity().getIs_collection())) {
            ivActivityCollect.setImageResource(R.mipmap.nearby_collect_r);
            ivRightCollect.setImageResource(R.mipmap.invite_collect_r);
            hasCollected = true;
        } else {
            ivActivityCollect.setImageResource(R.mipmap.nearby_collect);
            ivRightCollect.setImageResource(R.mipmap.invite_collect);
            hasCollected = false;
        }
        ivRightCancel.setVisibility(View.GONE);
        ivRightEdit.setVisibility(View.GONE);
        picList = new ArrayList<>();
        picList.clear();
        picList.add(inviteDetailDataBean.getActivity().getImg_path());
        List<InviteDetailBean.DataBean.ActivityBean.ImgOtherBean> imgOtherBeanList = inviteDetailDataBean.getActivity().getImg_other();
        if (imgOtherBeanList.size() > 0) {
            for (int i = 0; i < imgOtherBeanList.size(); i++) {
                picList.add(imgOtherBeanList.get(i).getUrl());
            }
        }
        initActivityPic();
        if ("1".equals(inviteDetailDataBean.getInvite().getIs_own())) {
            llActivityCancel.setVisibility(View.VISIBLE);
            llActivityCollect.setVisibility(View.GONE);
            isOwn = true;
        } else {
            llActivityCancel.setVisibility(View.GONE);
            llActivityCollect.setVisibility(View.VISIBLE);
            isOwn = false;
        }

        if ("1".equals(inviteDetailDataBean.getInvite().getIs_expire())) {
            outTime = true;
            tvSubmitInvitation.setText("已过期");
            tvSubmitInvitation.setEnabled(false);
            btnActivityJoin.setText("已过期");
            btnActivityJoin.setEnabled(false);
        } else {
            outTime = false;
            if ("2".equals(inviteDetailDataBean.getInvite().getIs_member())) {
                tvSubmitInvitation.setText("已取消");
                tvSubmitInvitation.setEnabled(false);
                btnActivityJoin.setText("已取消");
                btnActivityJoin.setEnabled(false);
            } else if ("1".equals(inviteDetailDataBean.getInvite().getIs_member())) {
                tvSubmitInvitation.setText("已参加");
                tvSubmitInvitation.setEnabled(false);
                btnActivityJoin.setText("已参加");
                btnActivityJoin.setEnabled(false);
            } else {
                if (inviteDetailDataBean.getActivity().getNumber_join().equals(inviteDetailDataBean.getActivity().getNumber_all())) {
                    tvSubmitInvitation.setText("人数已满");
                    tvSubmitInvitation.setEnabled(false);
                    btnActivityJoin.setText("人数已满");
                    btnActivityJoin.setEnabled(false);
                } else {
                    tvSubmitInvitation.setText("我要参加");
                    tvSubmitInvitation.setEnabled(true);
                    btnActivityJoin.setText("我要参加");
                    btnActivityJoin.setEnabled(true);
                }
            }
        }
    }

    public void inviteDetailFail(String error) {
        dismissLoading();
    }

    public void inviteCancelSucceed(String msg) {
//        showToast(msg, R.mipmap.nearby_success);
        T.getInstance().showShort(msg);
        BusProvider.getBus().post(new RefreshEvent());
        okDialogView.dismiss();
        Router.pop(this);

    }

    public void inviteCancelFail(String error) {
//        showToast(error, R.mipmap.jinshi);
        T.getInstance().showShort(error);


    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        UMShareAPI.get(this).onActivityResult(requestCode, resultCode, data);
        if (requestCode == EDITOR_USER_INFO && resultCode == RESULT_OK) {
            tvSubmitInvitation.setEnabled(false);
            btnActivityJoin.setEnabled(false);
            tvSubmitInvitation.setText("已参加");
            btnActivityJoin.setText("已参加");
            BusProvider.getBus().post(new RefreshEvent());

        }
    }

    @Override
    public void onScroll(int scrollViewPosition) {
        if (scrollViewPosition == 1) {
            if (type == EDITOR) {
                ivChangeBgButton.setVisibility(View.GONE);
                tvSubmitInvitation.setVisibility(View.VISIBLE);
                rlActivityMenu.setVisibility(View.GONE);
            } else {
                tvSubmitInvitation.setVisibility(View.GONE);
                rlActivityMenu.setVisibility(View.VISIBLE);
            }
        } else {
            if (type == EDITOR) {
                rlActivityMenu.setVisibility(View.GONE);
                ivChangeBgButton.setVisibility(View.VISIBLE);
                tvSubmitInvitation.setVisibility(View.VISIBLE);
            } else {
                rlActivityMenu.setVisibility(View.GONE);
                tvSubmitInvitation.setVisibility(View.VISIBLE);
            }
        }
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        tvPicNum.setText((position + 1) + "/" + picList.size());
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }

    public void upLoadImgSucceed(UploadImgBean.DataBean data) {
        List<UploadImgBean.DataBean.DetailBean> dataDetail = data.getDetail();
        StringBuffer stringBuffer = new StringBuffer();
        publishReqBean.setImg_path(dataDetail.get(0).getImg_path());
        for (int i = 0; i < dataDetail.size(); i++) {
            if (i != 0) {
                stringBuffer.append(dataDetail.get(i).getImgid());
                if (i != dataDetail.size() - 1) {
                    stringBuffer.append(",");
                }
            }
        }
        if (stringBuffer.length() > 0) {
            publishReqBean.setImg_other(stringBuffer.toString());
        }
        getP().publish(publishReqBean);
//
//        tvSubmitInvitation.setEnabled(true);
//        tvSubmitInvitation.setText("邀请");
    }

    public void upLoadImgFail(String error) {
//        showToast(error, R.mipmap.jinshi);
        dismissLoadingDialog();
        T.getInstance().showShort(error);
    }

    public void publishSucceed(PublishBean.DataBean data) {
//        dismissLoading();
        dismissLoadingDialog();
        ivChangeBgButton.setVisibility(View.GONE);
        ActivityManager.getInstance().finishAllActivity(EditorInvitedActivity.class);
        BusProvider.getBus().post(new RefreshEvent());
        share_url = data.getShare_url();
        initShare(share_url);
    }

    public void publishFail(String error) {
        T.getInstance().showShort(error);
//        showToast(error, R.mipmap.jinshi);
//        dismissLoading();
        dismissLoadingDialog();
    }


    public void collectionSucceed(String msg) {
        hasCollected = true;
//        showToast(msg, R.mipmap.nearby_success);
        T.getInstance().showShort(msg);

        ivRightCollect.setImageResource(R.mipmap.invite_collect_r);
        ivActivityCollect.setImageResource(R.mipmap.activity_collect_r);
        BusProvider.getBus().post(new CollectEvent(true));
    }

    public void collectionFail(String error) {
//        showToast(error, R.mipmap.jinshi);
        T.getInstance().showShort(error);

    }

    public void collectionCancelSucceed(String msg) {
        hasCollected = false;
//        showToast(msg, R.mipmap.nearby_success);
        T.getInstance().showShort(msg);
        ivRightCollect.setImageResource(R.mipmap.invite_collect);
        ivActivityCollect.setImageResource(R.mipmap.activity_collect);
        BusProvider.getBus().post(new CollectEvent(false));

    }

    public void collectionCancelFail(String error) {
//        showToast(error, R.mipmap.jinshi);
        T.getInstance().showShort(error);
    }

    public void inviteAcceptSucceed(String msg) {
        tvSubmitInvitation.setText("已参加");
        btnActivityJoin.setText("已参加");
        btnActivityJoin.setEnabled(false);
        tvSubmitInvitation.setEnabled(false);
    }

    public void inviteAcceptFail(String error) {
        T.getInstance().showShort(error);
//        showToast(error, R.mipmap.jinshi);
    }

    @Override
    public void onItemClick(int position) {
        Router.newIntent((Activity) App.getInstance())
                .putStringArrayList("pic", picList)
                .putInt("type", type)
                .putInt("position", position)
                .to(PicActivity.class)
                .launch();
    }

    @Override
    public void onItemLongClick(int position) {

    }

    public void getTypeListSucceed(List<GetTypeListBean.DataBean> dataBeanList) {
        typeList = dataBeanList;
        getP().getInvitation(typeList);
        typeList.get(0).setSelect(true);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
        rvBgType.setLayoutManager(linearLayoutManager);
        rvBgType.addItemDecoration(new SpacesItemDecoration(60));
        picTypeAdapter = new PicTypeAdapter(this, typeList);
        rvBgType.setAdapter(picTypeAdapter);
        picTypeAdapter.setOnItemClickListener(this);
    }

    public void getTypeListFail(String error) {
        dismissLoading();
    }

    @Override
    public void onClick(List<GetTypeListBean.DataBean> typeList, int position) {
        for (GetTypeListBean.DataBean bean : typeList) {
            bean.setSelect(false);
        }
        typeList.get(position).setSelect(true);
        this.typeList = typeList;
        picTypeAdapter.setData(this.typeList);
        picAdapter.setData(invitationList.get(position));
    }

    @Override
    public void onLongClick(List<GetTypeListBean.DataBean> typeList, int position) {

    }

    public void getInvitationFinish(List<List<InvitationBean.DataBean>> invitationList) {
        dismissLoading();
        this.invitationList = invitationList;
        rvBg.setAlpha(0.8f);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
        rvBg.setLayoutManager(linearLayoutManager);
        rvBg.addItemDecoration(new SpacesItemDecoration(60));
        picAdapter = new LetterBgPicAdapter(this, invitationList.get(0));
        rvBg.setAdapter(picAdapter);
        picAdapter.setOnItemClickListener(this);
        picSelectId = invitationList.get(0).get(0).getId();
        changeTextColor(invitationList.get(0).get(0).getColor());
        picAdapter.changeSelected(invitationList.get(0).get(0).getId());
        Picasso.with(this).load(invitationList.get(0).get(0).getAd_logo()).error(R.color.white).placeholder(R.color.white).into(ivBg, new Callback() {
            @Override
            public void onSuccess() {

                tvSubmitInvitation.setVisibility(View.VISIBLE);
                tvLetterThings.setText(publishReqBean.getTitle());
                tvLetterTime.setText(GetSystemTimeUtil.stamp2Date(publishReqBean.getInvite_time()));
                tvLetterWeek.setText(GetSystemTimeUtil.getWeek(GetSystemTimeUtil.stampToDate(publishReqBean.getInvite_time())));
                tvLetterWhere.setText(publishReqBean.getLandmark());
                tvLetterAddress.setText(publishReqBean.getAddress());
                tvLetterWho.setText("来自 " + getP().getLocalUserInfo(getP().getToken()).getUser_nickname() + "  的邀请");
                if ("1".equals(publishReqBean.getIs_activity())) {
                    tvActivityTitle.setText(publishReqBean.getTitle2());
                    tvActivityTime.setText(GetSystemTimeUtil.stamp22Date(publishReqBean.getInvite_time()));
                    tvActivityLocation.setText(publishReqBean.getLandmark());
                    tvActivityNum.setText("限" + publishReqBean.getNumber_all() + "人参加");
                    tvActivityRegistrationTime.setText(GetSystemTimeUtil.stamp2Date(publishReqBean.getTime_start()) + "-" + GetSystemTimeUtil.stamp2Date(publishReqBean.getTime_end()));
                    tvActivityDes.setText(publishReqBean.getContent());
                    ivActivityLocationNext.setVisibility(View.GONE);
                    ivActivityNumNext.setVisibility(View.GONE);
                }
            }

            @Override
            public void onError() {
                dismissLoadingDialog();

            }
        });

    }

    @Override
    public void onClick(InvitationBean.DataBean dataBean) {
        picSelectId = dataBean.getId();
        picAdapter.changeSelected(picSelectId);
        changeTextColor(dataBean.getColor());
        Picasso.with(InvitationLetterActivity.this).load(dataBean.getAd_logo()).error(R.color.white).placeholder(R.color.white).into(ivBg, new Callback() {
            @Override
            public void onSuccess() {
                dismissLoadingDialog();

            }

            @Override
            public void onError() {
                dismissLoadingDialog();
            }
        });
    }

    @Override
    public void onLongClick(InvitationBean.DataBean dataBean) {

    }


    private static class MyLocationListener implements BDLocationListener {
        @Override
        public void onReceiveLocation(BDLocation bdLocation) {
            //获取纬度信息
            latitude = bdLocation.getLatitude();
            //获取经度信息
            longitude = bdLocation.getLongitude();
        }


    }

    /**
     * 百度地图经纬度转为高德经纬度
     *
     * @param bd_lat
     * @param bd_lon
     * @return
     */
    private double[] bdToGaoDe(double bd_lat, double bd_lon) {
        double[] gd_lat_lon = new double[2];
        double PI = 3.14159265358979324 * 3000.0 / 180.0;
        double x = bd_lon - 0.0065, y = bd_lat - 0.006;
        double z = Math.sqrt(x * x + y * y) - 0.00002 * Math.sin(y * PI);
        double theta = Math.atan2(y, x) - 0.000003 * Math.cos(x * PI);
        gd_lat_lon[0] = z * Math.cos(theta);
        gd_lat_lon[1] = z * Math.sin(theta);
        return gd_lat_lon;
    }

    private boolean isInstallByRead(String packageName) {
        return new File("/data/data/" + packageName).exists();
    }
}
